<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>受控组件</title>
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <!-- 生产环境中不建议使用 -->
  <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>

<body>
  <!-- 容器 -->
  <div id="test"></div>

  <script type="text/babel">
    class Demo extends React.Component {
      state = {
        name: "",
        password: ""
      }
      // 表单提交回调
      handleSubmit = (event) => {
        // 禁止表单默认刷新操作
        event.preventDefault();
        let { name, password } = this.state;
        alert(`表单中姓名为${name}，密码是${password}`)
      }
      // 保存用户名到状态中
      saveName = (event) => {
        this.setState({ name: event.target.value })
      }
      // 保存密码到状态中
      savePassword = (event) => {
        this.setState({ password: event.target.value })
      }
      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            姓名： <input onChange={this.saveName} type="text" name="name" />
            密码： <input onChange={this.savePassword} type="password" name="password" />
            <button>按钮</button>
          </form>
        )
      }
    }

    //2.渲染虚拟DOM到页面
    ReactDOM.render(<Demo a="1" b="2" />, document.getElementById("test"))
  </script>

</body>

</html>
